package com.easy.frame.servers.sys.rabbit;

import com.easy.frame.base.basic.pojo.SysExceptionLog;
import com.easy.frame.base.service.rabbit.RabbitConstants;
import com.easy.frame.servers.sys.service.SysExceptionLogService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 异常日志消费类
 *
 * @author zzf
 * @date 2021/1/8 9:05
 */
@Component
public class RabbitReceive {

    @Autowired
    SysExceptionLogService exceptionLogService;

    /**
     * 监听异常队列，并保存
     */
    @RabbitHandler
    @RabbitListener(queues = RabbitConstants.Queue.EXCEPTION_HANDLER)
    public void onMessage(Message<SysExceptionLog> message, Channel channel) throws Exception {

        exceptionLogService.save(message.getPayload());

        //  2. 处理成功之后 获取deliveryTag 并进行手工的ACK操作, 因为我们配置文件里配置的是 手工签收
        //	spring.rabbitmq.listener.simple.acknowledge-mode=manual
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
    }
}
